package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.common.exception.ExceptionCast;
import com.changgou.common.model.response.goods.GoodsCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ESManagerMapper esManagerMapper;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex( SkuInfo.class );
        //创建映射
        elasticsearchTemplate.putMapping( SkuInfo.class );
    }

    //导入全部sku集合进入索引库
    @Override
    public void importAll() {
        //查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId( "all" );
        if(skuList == null || skuList.size()<=0){
            ExceptionCast.cast( GoodsCode.GOODS_SKU_SEARCH_ERROR );
        }
        //skuList转为json
        String jsonSkuList = JSON.toJSONString( skuList );
        //将json转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray( jsonSkuList, SkuInfo.class );

        //将规格转为map
        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject( skuInfo.getSpec(), Map.class );
            skuInfo.setSpecMap( specMap );
        }
        //导入索引库
        esManagerMapper.saveAll( skuInfoList );

    }

    //根据spuId查询skulist，添加到索引库
    @Override
    public void importDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId( spuId );
        for (Sku sku : skuList) {
            System.out.println("--------");
            System.out.println(sku);
        }
        if(skuList == null || skuList.size()<=0){
            ExceptionCast.cast( GoodsCode.GOODS_SKU_SEARCH_ERROR );
        }
        //将集合转为json
        String jsonSkuList = JSON.toJSONString( skuList );
        //将json转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray( jsonSkuList, SkuInfo.class );

        //将规格转为map
        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject( skuInfo.getSpec(), Map.class );
            skuInfo.setSpecMap( specMap );
        }

        //添加索引库
        esManagerMapper.saveAll( skuInfoList );
    }

    //根据spuId删除索引
    @Override
    public void delDataBySpuId(String spuId) {
        Iterable<SkuInfo> skuInfos = esManagerMapper.search( QueryBuilders.termQuery( "spuId", spuId ) );
        if (skuInfos!=null){
            esManagerMapper.deleteAll(skuInfos);
        }
        /*List<Sku> skuList = skuFeign.findSkuListBySpuId( spuId );
        if (skuList==null && skuList.size()<=0){
            ExceptionCast.cast( GoodsCode.GOODS_SKU_SEARCH_ERROR );
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById( Long.parseLong( sku.getId() ) );
        }*/
    }
}
